﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using Spring.Objects;
using System.Reflection;

/// <summary>
/// CollectionUtil 的摘要描述
/// </summary>
/// 
namespace com.pro2e.console.core.util
{
    public class CollectionUtil
    {
        public CollectionUtil()
        {

        }

        /// <summary>
        /// 移除collctinn項目前 值等於多少的項目
        /// </summary>
        /// <param name="enumerator"></param>
        /// <param name="keyFiledName"></param>
        public static void RemoveCollectionItem<T>(IList<T> iList,string keyFiledName,object value)
        {
            if (iList != null && iList.Count > 0) 
            {
                foreach (T obj in iList) 
                {
                    ObjectWrapper ow = new ObjectWrapper(obj);
                    PropertyInfo pinfo = ow.GetPropertyInfo(keyFiledName);

                    if (ow.GetPropertyValue(keyFiledName) != null && ow.GetPropertyValue(keyFiledName).ToString().Equals(value.ToString())) 
                    {
                        iList.Remove((T)ow.WrappedInstance);
                        break;
                    }
                }
            }
        }




        /// <summary>
        /// 判斷iList的VO欄位是否含特定的值(限比對string的屬性)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="iList"></param>
        /// <param name="filedName"></param>
        /// <param name="matchValue"></param>
        /// <returns></returns>
        public static bool ContainValue<T>(IList<T> iList, string filedName, string matchValue)
        {
            if (iList != null && iList.Count > 0)
            {
                foreach (T obj in iList)
                {
                    ObjectWrapper ow = new ObjectWrapper(obj);
                    string value = ow.GetPropertyValue(filedName) != null ? ow.GetPropertyValue(filedName).ToString() : null;
                    if (matchValue != null && matchValue.Equals(value))
                    {
                        return true;
                    }
                }
            }
            return false;
        }


        /// <summary>
        /// 判斷iList的VO欄位是否含特定的值的所有VO
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="iList"></param>
        /// <param name="filedName"></param>
        /// <param name="matchValue"></param>
        /// <returns></returns>
        public static IList<T> GetMatchedValueList<T>(IList<T> iList, string filedName, string matchValue)
        {
            IList<T> resutList = new List<T>();

            if (iList != null && iList.Count > 0)
            {
                foreach (T obj in iList)
                {
                    ObjectWrapper ow = new ObjectWrapper(obj);
                    string value = ow.GetPropertyValue(filedName) != null ? ow.GetPropertyValue(filedName).ToString() : null;
                    if (matchValue != null && matchValue.Equals(value))
                    {
                        resutList.Add(obj);
                    }
                }
            }

            if (resutList.Count > 0)
            {
                return resutList;
            }
            else 
            {
                return null;
            }

        }

        /// <summary>
        /// 判斷iList的VO欄位是否含特定的值的第一個VO
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="iList"></param>
        /// <param name="filedName"></param>
        /// <param name="matchValue"></param>
        /// <returns></returns>
        public static T GetFirstOneMatchedValueList<T>(IList<T> iList, string filedName, string matchValue)
        {
            if (iList != null && iList.Count > 0)
            {
                foreach (T obj in iList)
                {
                    ObjectWrapper ow = new ObjectWrapper(obj);
                    string value = ow.GetPropertyValue(filedName) != null ? ow.GetPropertyValue(filedName).ToString() : null;
                    if (matchValue != null && matchValue.Equals(value))
                    {
                        return obj;
                    }
                }
            }
            return  default(T);
        }

        /// <summary>
        /// 判斷在iEnumberator 是否包含某值
        /// </summary>
        /// <param name="iEnumerator"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool ContainValue(IEnumerator iEnumerator, object obj)
        {
            if (iEnumerator != null) 
            {
                while (iEnumerator.MoveNext()) 
                {
                    object inObj = iEnumerator.Current;
                    if (inObj.Equals(obj)) 
                    {
                        return true;
                    }                    
                }
            }
            return false;         
        }
    }





}